/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package visualizer;

import Bloques.Segment;
import carGeneration.Car;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author emmanuel
 */
public class FindEvent implements Runnable{

    private CiudadelaNet ciudadelaNet;
    private Segment[] segments;
     int SE = 1; //Stop Event
    int CE = 2;//Cross Event
    int LCE = 3; //Light Change Event
    int CLE = 5; //Change Lane Event
    int CLEE = 6; //Change Lane End Event
    int ALE = 7; //Arrival Link Event
    int LLE = 8; //Leave Link Event
    int BE = 9; //Begin Event
    int AE = 10; //Arrival Event
    int WSE = 11; //Warning Event, as a stop event, but dont stop
    int WBE = 12; // warning Event, as a begin event, but actually for a dont stop event
    
    public FindEvent(CiudadelaNet ciudadelaNet,Segment segments[]){
        this.ciudadelaNet =  ciudadelaNet;
        this.segments = segments;
    }
    public void run() {
       try{
        Thread.sleep(1000);
       }catch(InterruptedException evt){
           
       }
        
        while(segments[0] == null){
            System.out.println("Waiting");
        }
        try {
            BufferedReader entrada;
            String[] result = null;

            entrada = new BufferedReader(new FileReader("c:\\temp\\log.txt"));

            String s = new String();

            s = entrada.readLine(); // para eliminar el encabezado.
            while ((s = entrada.readLine()) != null) {
//				lectura de datos
                result = s.split(",");

               
                //entero,double,entero,double,entero
                //ObjectId, evTime,segment, evPOs,evType
                if (Integer.valueOf(result[4]).intValue() == SE || Integer.valueOf(result[4]).intValue() == ALE || Integer.valueOf(result[4]).intValue() == BE || Integer.valueOf(result[4]).intValue() == AE || Integer.valueOf(result[4]).intValue() == WSE || Integer.valueOf(result[4]).intValue() == WBE) {
                    //car(int id,double position,String name,double delayToStart,double velocity,double distMinSafe)
                    Car tmp = segments[Integer.valueOf(result[2]).intValue()].findCar(Integer.valueOf(result[0]).intValue());
                    if (tmp == null) {
                        segments[Integer.valueOf(result[2]).intValue()].putCar(new Car(Integer.valueOf(result[0]).intValue(), Double.valueOf(result[3]).doubleValue(), "", 0.0, 0.0, 0.0), Double.valueOf(result[3]).doubleValue());
                    } else {
                        segments[Integer.valueOf(result[2]).intValue()].putCar(tmp, Double.valueOf(result[3]).doubleValue());

                        // findNode(result[0],Double.valueOf(result[1]).doubleValue(),Integer.valueOf(result[2]).intValue(),Double.valueOf(result[3]).doubleValue());
                    }
                } else {
                    if (Integer.valueOf(result[4]).intValue() == CE) {
                        Car tmp = segments[Integer.valueOf(result[2]).intValue()].findCar(Integer.valueOf(result[0]).intValue());
                        if (tmp == null) {
                            System.out.println("Error fatal"); //
                        } else {
                            segments[Integer.valueOf(result[2]).intValue()].removeCar(tmp);
                        }
                    }
                }

                ciudadelaNet.repaint();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    break;
                }
            }
            entrada.close();
        } catch (IOException ex) {
            Logger.getLogger(FindEvent.class.getName()).log(Level.SEVERE, null, ex);
        }
		
    
    }

}
